package com.learner.ucenter.utils;

import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description:
 **/
public class SystemUtil {

    private static final OperatingSystemMXBean OPERATING_SYSTEM_BEAN;

    static {
        OPERATING_SYSTEM_BEAN = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    }

    public static double getSystemCpuLoad() {
        return OPERATING_SYSTEM_BEAN.getSystemCpuLoad();
    }

    public static int getSystemCpuCount() {
        return Runtime.getRuntime().availableProcessors();
    }

    public static double getSystemMemLoad() {
        double totalMem = OPERATING_SYSTEM_BEAN.getTotalPhysicalMemorySize();
        double freeMem = OPERATING_SYSTEM_BEAN.getFreePhysicalMemorySize();
        return (totalMem - freeMem) / totalMem;
    }
}